package com.lixin.smartclassroom.ui.home.tab2.activity

import android.annotation.SuppressLint
import android.graphics.Color
import com.drake.brv.utils.bindingAdapter
import com.drake.channel.receiveEvent
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.api.coursedagang
import com.lixin.smartclassroom.api.zhcoursedetail
import com.lixin.smartclassroom.api.zhuticoursedetail
import com.lixin.smartclassroom.bean.BaseBean
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.bean.Small
import com.lixin.smartclassroom.databinding.ActivitySmartCourseDetailBinding
import com.lixin.smartclassroom.ext.child_id
import com.lixin.smartclassroom.ext.user_role
import com.lixin.smartclassroom.ui.home.adapter.courseChapter
import com.lixin.smartclassroom.ui.home.adapter.oneText
import com.lixin.smartclassroom.ui.home.tab4.activity.SetGroupAct
import com.lixin.smartclassroom.ui.home.tab5.activity.QuestionnaireDetailAct
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.dp2px
import com.lixinkeji.extension.getIntentString
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.load
import com.lixinkeji.extension.loadCircle
import com.lixinkeji.extension.loadRound
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.removeAt
import com.lixinkeji.extension.round
import com.lixinkeji.extension.setRoundRectBg
import com.lixinkeji.extension.start
import com.lixinkeji.extension.visible
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import java.math.BigDecimal

/**
 * Description 智慧详情
 * Author: lwc
 * Date:2023/12/6 15:00
 */
class SmartCourseDetailAct : BaseActivity<ActivitySmartCourseDetailBinding>() {
    override fun getViewBinding() = ActivitySmartCourseDetailBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    private lateinit var listBean: ArrayList<DataListBean>
    override fun title() = "详情"
    private var taskType = ""
    private var courseid = ""
    private var baseBean: BaseBean? = null


    override fun iniView() {
        courseid = getIntentString("courseid")
        taskType = getIntentString("type")
        getCourseData()
        getCourseChapterData()
        binding.apply {
            tvSubject.setRoundRectBg(Color.parseColor("#009B93"), dp2px(5f).toInt())

            when (user_role) {
                1 -> {
                    tvProgress.visible()
                    progressbar.visible()
                    tvEvaluateTeacher.visible()
                }

                2 -> {
                    tvProgress.visible()
                    progressbar.visible()
                    tvEvaluateTeacher.visible()
                }

                3 -> {
                    llStudent.gone()
                    llTeacher.visible()
                    tvProgress.gone()
                    progressbar.gone()
                    tvEvaluateTeacher.gone()

                    when (taskType) {
                        "1" -> {
                            llContent.visible()
                            llYanxue.gone()
                            ratingBar.gone()
                            tvLevel.gone()
                        }

                        "2" -> {
                            llContent.visible()
                            llYanxue.gone()
                            ratingBar.gone()
                            tvLevel.gone()
                        }

                        "3" -> {
                            llContent.gone()
                            llYanxue.visible()
                            tvDate.visible()
                            ratingBar.visible()
                            tvLevel.visible()
                        }
                    }


                    val listBeans = ArrayList<DataListBean>()
                    listBeans.add(DataListBean())
                    listBeans.add(DataListBean())
                    rcvLabel.oneText().models = listBeans

                    if (taskType == "3") {
                        btnYanXueGroup.visible()
                        btnAddChapter.gone()
                    }
                }
            }


            smartRefreshLayout.setOnRefreshListener(object : OnRefreshListener {
                override fun onRefresh(refreshLayout: RefreshLayout) {
                    getCourseData()
                    getCourseChapterData()
                }

            })
            smartRefreshLayout.setEnableLoadMore(false)


        }

        receiveEvent<String>("refreshDaGang") {
            getCourseData()
            getCourseChapterData()
        }

        receiveEvent<Int>("deleteBigChapter") {
            binding.recycler.bindingAdapter.removeAt(it)
        }

        receiveEvent<Small>("deleteSmallChapter") {
            for (i in 0 until listBean[it.parentPosition!!].small?.size!!) {
                if (listBean[it.parentPosition!!].small?.get(i)?.id == it.id) {
                    (listBean[it.parentPosition!!].small as ArrayList).removeAt(i)
                    binding.recycler.bindingAdapter.notifyDataSetChanged()
                    return@receiveEvent
                }
            }
        }

        receiveEvent<Int>("openOrCloseBigChapter") {
            when ((binding.recycler.bindingAdapter.models?.get(it) as DataListBean).state) {
                "1" -> (binding.recycler.bindingAdapter.models?.get(it) as DataListBean).state = "2"
                "2" -> (binding.recycler.bindingAdapter.models?.get(it) as DataListBean).state = "1"
            }
            binding.recycler.bindingAdapter.notifyDataSetChanged()
        }

        receiveEvent<Small>("openOrCloseSmallChapter") {
            for (i in 0 until listBean[it.parentPosition!!].small?.size!!) {
                if (listBean[it.parentPosition!!].small?.get(i)?.id == it.id) {
                    when (listBean[it.parentPosition!!].small?.get(i)?.state) {
                        "1" -> listBean[it.parentPosition!!].small?.get(i)?.state = "2"
                        "2" -> listBean[it.parentPosition!!].small?.get(i)?.state = "1"
                    }
                    binding.recycler.bindingAdapter.notifyDataSetChanged()
                    return@receiveEvent
                }
            }
        }


    }

    private fun getCourseChapterData() {
        binding.apply {
            scopeNetLife {
                listBean = when (user_role) {
                    1 -> coursedagang(courseid, taskType, child_id)!!
                    else -> coursedagang(courseid, taskType)!!
                }

                tvChapterCount.text = "课程大纲(" + listBean.size + ")"
                recycler.courseChapter(taskType).models = listBean
                if (smartRefreshLayout.isRefreshing)
                    smartRefreshLayout.finishRefresh()
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun getCourseData() {
        binding.apply {
            scopeNetLife {
                when (taskType) {
                    "1" -> baseBean = when (user_role) {
                        1 -> zhcoursedetail(courseid, child_id)
                        else -> zhcoursedetail(courseid)
                    }

                    "2" -> baseBean = when (user_role) {
                        1 -> zhuticoursedetail(courseid, child_id)
                        else -> zhuticoursedetail(courseid)
                    }
                }

                if (baseBean!!.result == "0") {
                    img.load(baseBean!!.coerimage!!)
                    tvSubject.text = baseBean!!.lablename
                    tvTitle.text = baseBean!!.title
                    tvTime.text = "发布时间：" + baseBean!!.fabutime
                    tvContent.text =
                        baseBean!!.xiaojiecount + "小节·" + baseBean!!.canyucount + "人参与"
                    tvClass.text = baseBean!!.classesname
                    tvTeacherName.text = baseBean!!.teachername
                    imgTeacher.loadCircle(baseBean!!.teacherimage!!)
                    if (baseBean!!.jindu?.isNotEmpty() == true) {
                        progressbar.progress = baseBean!!.jindu?.toDouble()?.toInt()!!
                        tvProgress.text = baseBean!!.jindu
                    }


                    when (taskType) {
                        "1" -> {
                            if (baseBean!!.yidaxiaojiecount?.isEmpty() == true) {
                                progressbar.progress = 0
                                tvProgress.text = "0"
                            } else {
                                if (baseBean!!.xiaojiecount!!.toInt() == 0) {
                                    progressbar.progress = 0
                                    tvProgress.text = "0"
                                } else {
                                    progressbar.progress =
                                        ((baseBean!!.yidaxiaojiecount!!.toDouble() / baseBean!!.xiaojiecount!!.toDouble()).round(2,
                                            BigDecimal.ROUND_HALF_UP)*100).toInt()
                                    tvProgress.text =
                                        progressbar.progress.toString()+ "%"
                                }
                            }
                        }

                        "2" -> {
                            if (baseBean!!.yilianxiaojie?.isEmpty() == true) {
                                progressbar.progress = 0
                                tvProgress.text = "0"
                            } else {
                                if (baseBean!!.yilianxiaojie!!.toInt() == 0) {
                                    progressbar.progress = 0
                                    tvProgress.text = "0"
                                } else {

                                    progressbar.progress =
                                        ((baseBean!!.yilianxiaojie!!.toDouble() / baseBean!!.xiaojiecount!!.toDouble()).round(2,
                                            BigDecimal.ROUND_HALF_UP)*100).toInt()
                                    tvProgress.text =
                                        progressbar.progress.toString()+ "%"
                                }
                            }
                        }

                    }



                    when (taskType) {
                        "1" -> {
                            tvylxjs.text = baseBean!!.yidaxiaojiecount
                            tvBriefCount.text = "/" + baseBean!!.xiaojiecount
                            tvYdts.text = baseBean!!.yidatimushuliang
                            tvScore.text = baseBean!!.zongdefen
                        }

                        "2" -> {
                            tvylxjs.text = baseBean!!.yilianxiaojie
                            tvBriefCount.text = "/" + baseBean!!.xiaojiecount
                            tvYdts.text = baseBean!!.daticount
                            tvScore.text = baseBean!!.totalsource
                            if (baseBean!!.icon?.isNotEmpty() == true&&baseBean!!.xiaojiecount==baseBean!!.yilianxiaojie) {
                                ivFinish.visible()
                                ivFinish.loadRound(baseBean!!.icon!!)
                            } else {
                                ivFinish.gone()
                            }
                        }
                    }
                }
            }
        }
    }


    override fun onClick() {
        binding.apply {
            btnAllGroup.onClick {
                start(AllGroupAct::class.java) {
                    putExtra("courseid", courseid)
                }
            }

            btnAddChapter.onClick {
                start(AddChapterAct::class.java) {
                    putExtra("type", taskType)
                    putExtra("courseid", courseid)
                }
            }
            btnReview.onClick {
                start(CourseReviewAct::class.java) {
                    putExtra("courseid", courseid)
                }
            }
            btnYanXueGroup.onClick {
                start(SetGroupAct::class.java)
            }
            btnPermission.onClick {
                start(CoursePermissionAct::class.java) {
                    putExtra("courseid", courseid)
                    putExtra("type", taskType)
                    putExtra("quanxian", baseBean!!.quanxian)
                    putExtra("customerids", baseBean!!.customerids)
                }
            }

            btnSignManage.onClick {
                start(SignManageAct::class.java)
            }

            tvEvaluateTeacher.onClick {
                start(QuestionnaireDetailAct::class.java) {
                    putExtra("type", 2)
                    putExtra("teacherid",baseBean?.teacherId)
                }
            }
        }
    }

}